<script>
	export default {
		onLaunch: async function(e) {
			console.log(e.query)
			if (e.query.f_parentid) {
				uni.storage.set("f_parentid", e.query.f_parentid)
			}
			if (e.query.c_parentid) {
				uni.storage.set("c_parentid", e.query.c_parentid)
			}
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary');
			const dom = weex.requireModule('dom');
			dom.addRule('fontFace', {
				'fontFamily': "graceIconfont",
				'src': "url('/Grace6/font/grace.ttf')"
			});
			// #endif

			if (uni.storage.get("xiaoqu")) {
				this.$store.commit("set_xiaoqu", uni.storage.get("xiaoqu"))
			}


			let flag = await this.login();

			if (flag == 0) {
				let userinfo = await uni.gPost("userinfo");
				if (!userinfo) uni.storage.clear("token");
				this.$store.commit("set_user", userinfo);
			}

			uni.$on("getLocation", () => {
				this.getLocation();
			})

			let base = await uni.gPost("base/info");
			if (base == "已过期") {
				wx.showModal({
					title: '提示',
					content: '您的系统已停用，请联系技术人员处理！17316621879',
					showCancel: true, // 是否显示取消按钮，默认为 true
					cancelText: '取消', // 取消按钮的文字，默认为"取消"
					cancelColor: '#000000', // 取消按钮的文字颜色，默认为"#000000"
					confirmText: '确定', // 确定按钮的文字，默认为"确定"
					confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为"#3CC51F"
					success(res) {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: "17316621879"
							})
						} else if (res.cancel) {
							return;
						}
					}
				});
				return;
			}
			this.$store.commit("set_base", base);

			this.getLocation();


			let menus = await uni.gPost("menu/tabbar");
			this.$store.commit("set_menus", menus)



			//#ifdef H5
			if (uni.$jswx.isWechat()) {
				let result = await uni.$jswx.initJssdk();
				if (result) {
					this.$store.commit("set_jssdk_status", 1);
					uni.storage.set("appId", result.appId);
				}
			}
			//#endif

			this.$isResolve()
		},
		onShow: function() {
		},
		onHide: function() {
		},


		methods: {

			getLocation() {
				let base = this.$store.state.base;
				if (base.show_base && base.show_base.is_dingwei) {
					uni.location.getLocation().then((location) => {
						if (location) {
							this.$store.commit("set_location", location);

							const xPI = (3.14159265358979324 * 3000.0) / 180.0;
							const PI = 3.1415926535897932384626;
							const a = 6378245.0;
							const ee = 0.00669342162296594323;
							const lng = location.longitude;
							const lat = location.latitude;
							let dlat = this.transformlat(lng - 105.0, lat - 35.0);
							let dlng = this.transformlng(lng - 105.0, lat - 35.0);
							let radlat = (lat / 180.0) * PI;
							let magic = Math.sin(radlat);
							magic = 1 - ee * magic * magic;
							let sqrtmagic = Math.sqrt(magic);
							dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
							dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
							let mglat = lat + dlat;
							let mglng = lng + dlng;

							let z = Math.sqrt(mglng * mglng + mglat * mglat) + 0.00002 * Math.sin(mglat * xPI);
							let theta = Math.atan2(mglat, mglng) + 0.000003 * Math.cos(mglng * xPI);
							let longitudes = z * Math.cos(theta) + 0.0065;
							let latitudes = z * Math.sin(theta) + 0.006;
							let longitude = longitudes.toFixed(6);
							let latitude = latitudes.toFixed(6);
							var long = latitude + '###' + longitude;
							// uni.showToast({
							//  	title: long,
							//  	icon: 'none',
							//    duration:40000
							//  });
							uni.gPost("base/location", {
								latitude: latitude,
								longitude: longitude
							}).then((address) => {
								this.$store.commit("set_city", address.city);
								this.$store.commit("set_district", address.district);
								this.address = address;

								if (base.city_type == 1) {
									let xiaoqu = uni.storage.get("xiaoqu");
									uni.gPost("xiaoqu/list", {
										latitude: latitude,
										longitude: longitude,
										region: address.district,
										page_size: 1,
										uuid: xiaoqu ? xiaoqu.uid : ''
									}).then((res) => {
										this.$store.commit("set_xiaoqu", res.results[0]);
									})
								}
							})
							uni.storage.set("location", {
								latitude: latitude,
								longitude: longitude
							})



						}
					}).catch((e) => {
						uni.showToast({
							title: "定位失败!",
							icon: 'none'

						})
						console.log(e)
					})
				}
			},
			login() {
				let that = this;
				return new Promise((resovle) => {
					//#ifdef MP-WEIXIN
					// 微信小程序登录
					uni.login({
						provider: 'weixin',
						success: function(res) {
							console.log(res);
							let code = res.code;
							uni.gPost("mp/login", {
								f_parentid: uni.storage.get("f_parentid"),
								code: code,
								platform: 'weixin'
							}).then((res) => {
								console.log(res)
								uni.storage.set("openid", res.openid);
								if (res.token) {
									//小程序登陆过的自动登录
									uni.storage.set("token", res.token);
									uni.storage.set("userinfo", res.userinfo);
									that.$store.commit("set_user", res.userinfo);
									return resovle(1);
								} else {
									return resovle(0);
								}
							})
						}
					});
					//#endif

					//#ifdef MP-TOUTIAO
					// 抖音小程序登录
					uni.login({
						provider: 'toutiao',
						success: function(res) {
							console.log('抖音登录成功:', res);
							let code = res.code;
							uni.gPost("mp/login", {
								f_parentid: uni.storage.get("f_parentid"),
								code: code,
								platform: 'douyin'
							}).then((res) => {
								console.log('抖音登录响应:', res)
								uni.storage.set("openid", res.openid);
								if (res.token) {
									//小程序登陆过的自动登录
									uni.storage.set("token", res.token);
									uni.storage.set("userinfo", res.userinfo);
									that.$store.commit("set_user", res.userinfo);
									return resovle(1);
								} else {
									// 抖音登录成功但用户未绑定，直接返回0
									// 用户点击"我的"时会自动触发抖音登录
									return resovle(0);
								}
							}).catch((error) => {
								console.error('抖音登录错误:', error);
								return resovle(0);
							});
						},
						fail: function(error) {
							console.error('抖音登录失败:', error);
							return resovle(0);
						}
					});
					//#endif

					// #ifndef MP
					return resovle(0);
					// #endif
				})
			},
			transformlat(lng, lat) {
				const PI = 3.1415926535897932384626;
				let ret = -100.0 +
					2.0 * lng +
					3.0 * lat +
					0.2 * lat * lat +
					0.1 * lng * lat +
					0.2 * Math.sqrt(Math.abs(lng));
				ret +=
					((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
						2.0) /
					3.0;
				ret +=
					((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) /
					3.0;
				ret +=
					((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) *
						2.0) /
					3.0;
				return ret;
			},
			transformlng(lng, lat) {
				const PI = 3.1415926535897932384626;
				let ret =
					300.0 +
					lng +
					2.0 * lat +
					0.1 * lng * lng +
					0.1 * lng * lat +
					0.1 * Math.sqrt(Math.abs(lng));
				ret +=
					((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
						2.0) /
					3.0;
				ret +=
					((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) /
					3.0;
				ret +=
					((150.0 * Math.sin((lng / 12.0) * PI) +
							300.0 * Math.sin((lng / 30.0) * PI)) *
						2.0) /
					3.0;
				return ret;
			}
		},
	}
</script>
<style lang="scss">
	/* 加载框架核心样式 */
	@import "./Grace6/css/grace.scss";

	@import "./Grace6/css/colors.scss";
	/* 加载自定义样式 */
	@import "./custom/custom.scss";



	/* 加载图标字体 - 条件编译模式 */
	/* #ifdef APP-PLUS-NVUE */
	.gui-icons {
		font-family: graceIconfont;
	}

	/* #endif */
	/* #ifndef APP-NVUE */
	page {
		background: #f8f8f8;
	}


	

	/* #endif */
</style>